Skip to content

test(tracking): add integration test for imagify_after_optimize hook#1197

Open
faisalahammad wants to merge 10 commits into
wp-media:developfrom
faisalahammad:fix/1159-integration-test-for-imagify-after-optimize
Open

test(tracking): add integration test for imagify_after_optimize hook#1197
faisalahammad wants to merge 10 commits into
wp-media:developfrom
faisalahammad:fix/1159-integration-test-for-imagify-after-optimize

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Description

Integration test that fires the imagify_after_optimize hook and verifies the Tracking Subscriber callback runs end to end. Resolves the gap where existing tests only asserted registration via has_action() without exercising the full hook-to-callback chain.

Summary

Adds an integration test for the imagify_after_optimize hook that verifies the Tracking Subscriber is invoked through the full event chain. The existing Test_GetSubscribers test only checked that the Subscriber was registered via has_action(), but never fired the action to confirm the callback actually runs end to end.

Closes #1159

Changes

Tests/Integration/classes/Tracking/Subscriber/Test_OptimizeHookIntegration.php

Before:
No test fired imagify_after_optimize. Subscriber registration was asserted statically only.
After:

public function set_up(): void {
    parent::set_up();
    $container   = apply_filters( imagify_container, null );
    $subscriber  = $container->get( Subscriber::class );
    $this->mock_tracking = $this->createMock( Tracking::class );

    $tracking_property = new \ReflectionProperty( Subscriber::class, tracking );
    $tracking_property->setAccessible( true );
    $tracking_property->setValue( $subscriber, $this->mock_tracking );
}

public function testFiringImagifyAfterOptimizeCallsTrackMediaOptimized(): void {
    // ...
    $this->mock_tracking->expects( $this->once() )
        ->method( track_media_optimized )
        ->with( $mock_process, $item );

    do_action( imagify_after_optimize, $mock_process, $item );
}

Why: Resolves the real Subscriber from the DI container and swaps only the Tracking dependency for a mock, so the Subscriber itself is not mocked. Firing do_action() exercises the complete path: ServiceProvider registration, EventManager wiring, and the Subscriber callback. The PHPUnit mock expectation auto-verifies the delegation.

Testing

Test 1: Hook fires and Subscriber callback runs

  1. Run composer test-integration -- --filter="Test_OptimizeHookIntegration"
    Result: test passes; track_media_optimized() is called once with the expected process and item.

When Imagify is installed via Composer as a dependency (e.g. in Bedrock),
the plugin's post-install-cmd scripts don't run, so Strauss prefixing
never executes. This leaves vendor/ with unprefixed League\Container classes
while code references prefixed Imagify\Dependencies\League\Container\*.

Fix: Add class_alias fallbacks in inc/main.php to map unprefixed classes
to prefixed namespace when needed. Works for both root package install
(prefixed classes exist) and dependency install (unprefixed only).

Fixes wp-media#1073
The Optimize/Restore buttons on the Edit Media page triggered a full
page reload through admin-post.php, and the "Optimizing..." state
never cleared until the page was reloaded manually.

- Wrap the meta-box buttons in imagify-data-actions-container with
  data-id and data-context so the existing media-modal.js click
  handler can scope them.
- Render the Optimize button through the button/optimize template so
  it carries the button-imagify-optimize class the JS binds to.
- Enqueue the media-modal script and print the button/processing JS
  template in the footer on the attachment edition screen.

Reuses the existing AJAX handlers, Imagifybeat polling and button
templates. No JavaScript changes. The admin-post URLs are unchanged,
so the page still works with JavaScript disabled.

Fixes wp-media#434
… guards

- Restore `use Imagify\Dependencies\League\Container\Container;` so bare
  `new Container()` resolves to the prefixed FQN in `imagify_init()`. The
  previous PR added `class_alias()` fallbacks but removed the `use` import,
  which broke namespace resolution; PHPUnit integration bootstrap fataled
  with 'Class "Container" not found' on every PHP matrix entry, and the
  same fatal killed admin render during Playwright E2E login.
- Drop the `false` second arg on the three `class_exists` /
  `interface_exists` guards in `inc/main.php` so Composer autoload runs
  before the alias fallback is evaluated (CodeRabbit critical).

Errors fixed:
- 'Class "Container" not found' (PHPUnit integration matrix PHP 7.4 +
  8.0-8.4; downstream Playwright E2E login timeout)
- CodeRabbit CRITICAL: autoload disabled by `false` arg

PHP 7.4+ compatible. PR Checklist still requires `# Description` section
on the PR body (handled separately via gh pr edit).

Refs wp-media#1196
…lection deprecation

Skip 3 ImagifyUser API-dependent integration tests when
IMAGIFY_TESTS_API_KEY is empty (e.g. fork PRs). These tests
hit live app.imagify.io endpoint; without repo secrets they
always produce WP_Error responses, failing 3 assertions.

Also fix ReflectionProperty::setValue() single-arg call for
static properties to avoid PHP 8.4 deprecation (risky tests).

Refs wp-media#1196
Replace hard-fail expect(env).toBeTruthy() pattern with test.skip()
in 4 Playwright specs that need IMAGIFY_TESTS_API_KEY. Same
approach as PHPUnit's markTestSkipped in commit 16ef5ea.

On fork PRs without repo secrets, these 10 tests skip instead of
failing. On upstream PRs with the key, all 10 run and assert normally.

Refs wp-media#1196
In-branch test.skip() in fix eb5e3ca did not halt the test when
checkbox is present in DOM but hidden (no-key branch). Promote
skip to top-level guard so fork PRs skip cleanly. Also add
toBeVisible assertion to fail fast and informatively when
checkbox is not rendered.

Refs wp-media#1196
CODACY_PROJECT_TOKEN is empty on fork PRs (GitHub secret policy), causing
the codacy-coverage-reporter binary to hard-fail. Gate the upload step
behind a repo-name match so forks get a passing 'skipped' status while
internal PRs still upload coverage.

Fixes PR wp-media#1196 Code Coverage failure on fork.
Add an integration test that fires the imagify_after_optimize action and
verifies the Tracking Subscriber callback runs. Resolves the coverage gap
where existing tests only checked Subscriber registration with has_action()
but never exercised the full hook to event chain.

The test resolves the real Subscriber from the DI container, swaps the
Tracking dependency for a PHPUnit mock, then fires do_action(). The mock
expects track_media_optimized() to be called once with the matching
process and item, proving the chain works end to end without mocking the
Subscriber itself.

Refs wp-media#1159
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add integration test for imagify_after_optimize hook

1 participant